(function(){
	var AjaxCrud = QualityCheck.AjaxCRUD;
	var serverUrl = QualityCheck.GPUD.ServiceConfig;
	var currentPage = 1;
	var pageSize = 10;
	var totalSize;
	var now = 0;
	var dataCache = [];
	$(function(){
		var usercode = $("#user-name",parent.parent.document).attr("code");
		var userkey = $("#user-name",parent.parent.document).attr("key");
		var usertype = $("#user-name",parent.parent.document).attr("usertype");
		keyCheck(usercode,usertype,userkey,function(){
			initSmPages();
			window.onresize = function () {
				initSmPages();
			};
			pagebtns();
			initGradeClass();
		});
	});
	function initGradeClass(){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var userRoles = $("#user-name",parent.parent.document).attr("rolecodes");
		var data = {
				cmd:"getDataRole",
				model:"license",
				userKey:userKey,
				data:{
					rolecode:userRoles
				},
				remark:"",
		};
		AjaxCrud.get2(serverUrl.ServerLoginUrl,data,function(res){
			var typeOpt = "<option value='' >学段</option>";
			if(res.result == 1 && res.data != undefined && res.data != ""){
				var data = res.data;
				for(var i = 0; i < data.length; i++){
					var name = "";
					if(data[i].type == "0"){
						name = "幼儿园";
					}else if(data[i].type == "1"){
						name = "小学";
					}else if(data[i].type == "2"){
						name = "中学";
					}else if(data[i].type == "3"){
						name = "高中";
					}
					typeOpt += "<option value='"+data[i].type+"'>"+name+"</option>";
				}
				$("#student-type").html(typeOpt);
				
				$("#student-type").unbind("change");
				$("#student-type").change(function(){
					var gradeData = null;
					var type = $("#student-type option:selected").val();
					var gradeOpt = "<option value='' classcodes='' classes=''>年级</option>";
					if(type != ""){
						for(var i = 0; i < data.length; i++){
							if(data[i].type == type){
								gradeData = data[i].data;
							}
						}
						for(var i = 0; i < gradeData.length; i++){
							gradeOpt += "<option value='"+gradeData[i].grade+"' classcodes='"+gradeData[i].classcode+"' classes='"+gradeData[i].classes+"'>"+gradeData[i].grade+"年级</option>";
						}
					}
					$("#student-grade").html(gradeOpt);
					
					var classOpt = "<option value='' classes=''>班级</option>";
					$("#student-classcode").html(classOpt);
					initClass(1,pageSize);
				});
				
				$("#student-grade").unbind("change");
				$("#student-grade").change(function(){
					var classes = $("#student-grade option:selected").attr("classes").split(",");
					var classcodes = $("#student-grade option:selected").attr("classcodes").split(",");
					var classOpt = "";
					for(var j = 0; j < classes.length; j++){
						if(classcodes[j] == "" && classes[j] == ""){
							classOpt += "<option value='' classes=''>班级</option>";
						}else{
							classOpt += "<option value='"+classcodes[j]+"' classes='"+classes[j]+"'>"+classes[j]+"班</option>";
						}
					}
					$("#student-classcode").html(classOpt);
					initClass(1,pageSize);
				});
				$("#student-classcode").unbind("change");
				$("#student-classcode").change(function(){
					initClass(1,pageSize);
				});
			}else{
				
			}
			initClass(1,pageSize);
		},null,null);
	}

	function initClass(currentpage,pagesize){
		var userKey = $("#user-name",parent.parent.document).attr("key");		
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var rolecode = $("#user-name",parent.parent.document).attr("rolecodes");
		var type = $("#student-type option:selected").val();
		var classcode = $("#student-classcode option:selected").val();
		var name = $("#class-name").val();
		var data = {
				cmd:"getAllClassByPagesize",
				model:"tclass",
				userKey:userKey,
				data:{
					type:type,
					classcode:classcode,
					name:name,
					currentpage:currentpage,
					pagesize:pagesize,
					schoolcode:schoolcode,
					rolecode:rolecode
				},
				remark:"",
		};
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			var content = "";
			var pagecontent = "<div class=\"pages-btn-next\">&gt;&gt;</div>";
			if(res.result == 1 && res.data != undefined && res.data != ""){
				var data = res.data.data;
				var total = res.data.total;
				totalSize = Math.ceil(total/pagesize);
				if(totalSize <= 5 && totalSize >0){
					for(var i = totalSize-1 ; i >= 0; i--){
						if(i == currentpage-1){
							pagecontent += "<div class=\"pages-btn page-btn-select\">"+(i + 1)+"</div>"; 
						}else{
							pagecontent += "<div class=\"pages-btn\">"+(i + 1)+"</div>"; 
						}
					}
				}else if(totalSize == 0){
					pagecontent += "<div class=\"pages-btn\">1</div>";
				}else if(totalSize > 5){
					var lastp = (now*5)+5;
					if(lastp > totalSize){
						lastp = totalSize;
					}
					for(var i = lastp-1 ; i >= (now*5); i--){
						if(i == currentpage-1){
							pagecontent += "<div class=\"pages-btn page-btn-select\">"+(i + 1)+"</div>"; 
						}else{
							pagecontent += "<div class=\"pages-btn\">"+(i + 1)+"</div>"; 
						}
					}
				}
				pagecontent += "<div class=\"pages-btn-last\">&lt;&lt;</div>";
				$("#student-page").empty();
				$("#student-page").append(pagecontent);
				
				$(".pages-btn").unbind("click");
				$(".pages-btn").click(function(){
					$(".pages-btn").removeClass("page-btn-select");
					$(this).addClass("page-btn-select");
					currentPage = parseInt($(this).text());
					initClass(currentPage,pageSize);
				});
				
				$(".pages-btn-last").unbind("click");
				$(".pages-btn-last").click(function(){
					if(totalSize <= 5 && totalSize >0){
						return false;
					}else if(totalSize == 0){
						return false;
					}else if(totalSize > 5){
						if(now > 0){
							now --;
							initClass((now*5+1),pageSize);
						}else{
							return false;
						}
					}
				});
				
				$(".pages-btn-next").unbind("click");
				$(".pages-btn-next").click(function(){
					if(totalSize <= 5 && totalSize >0){
						return false;
					}else if(totalSize == 0){
						return false;
					}else if(totalSize > 5){
						if(totalSize%5 == 0){
							if((now+1) < totalSize/5){
								now ++;
								initClass((now*5+1),pageSize);
							}else{
								return false;
							}
						}else{
							if((now+1) <= (totalSize-totalSize%5)/5){
								now ++;
								initClass((now*5+1),pageSize);
							}else{
								return false;
							}
						}
						
					}
				});
				if(data.length > 0){
					dataCache = [];
					for(var i = 0; i < data.length; i++){
						for(var key in data[i]){
							if(data[i][key] == null || data[i][key] == "null" || data[i][key] == undefined){
								data[i][key] = "";
							}
							for(var key2 in data[i]["teacher"]){
								if(data[i]["teacher"][key2] == null || data[i]["teacher"][key2] == "null" || data[i]["teacher"][key2] == undefined){
									data[i]["teacher"][key2] = "";
								}
							}
						}
						var className = data[i].grade + "年级" + data[i].class_ + "班";
						content += "<div class=\"table-trs\">" +
							"<div class=\"tables-left\" style='margin-left:15px;'>" +
								"<div class=\"table-name\">"+className+"</div>" +
								"<div class=\"table-code\">"+data[i].classcode+"</div>" +
							"</div>" +
							"<div class=\"tables-middle\">" +
								"<div class=\"table-class\" title='"+data[i].teacher.name+"' style='width:150px;'>"+data[i].teacher.name+"</div>" +
								"<div class=\"table-code\" style='width:150px;'>"+data[i].teacher.work+"</div>" +
							"</div>" +
							"<div class=\"tables-right\">" +
								"<div class=\"table-address\">"+data[i].teacher.cellphone+"</div>" +
								"<div class=\"table-oprate\">" +
									"<div class=\"table-detail\" dataindex = \""+i+"\" classcode=\""+data[i].classcode+"\" title=\"详情\"></div>" +
									"<div class=\"table-edit\" dataindex = \""+i+"\" classcode=\""+data[i].classcode+"\" title=\"编辑\"></div>" +
									"<div class=\"table-delete\" classcode=\""+data[i].classcode+"\" title=\"删除\"></div>" +
									"<div class=\"table-modelpower\" dataindex = \""+i+"\" classcode=\""+data[i].classcode+"\" title=\"课程表\" classname=\""+className+"\" style='margin-left:15px;'></div>" +
								"</div>" +
							"</div>" +
						"</div>";
						dataCache.push(data[i]);
					}
					$("#class-data-show").empty();
					$("#class-data-show").append(content);
					
					$(".table-trs").unbind("hover");
					$(".table-trs").hover(function(){
						$(this).find(".table-oprate").show();
					},function(){
						$(".table-oprate").hide();
					});
					
					$(".table-detail").unbind("click");
					$(".table-detail").click(function(){
						var classcode = parseInt($(this).attr("classcode"));
						var index = parseInt($(this).attr("dataindex"));
						$("#class-form-show").show();
						$("#right-tables").hide();
						$("#class-form-show").contents().find("input").attr("disabled","disabled");
						$("#class-form-show").contents().find("input").css("background-color","#E5E5E5");
						$("#class-form-show").contents().find("select").attr("disabled","disabled");
						$("#class-form-show").contents().find("select").css("background-color","#E5E5E5");
						$("#class-form-show").contents().find("#back-to-table").unbind("click");
						$("#class-form-show").contents().find("#back-to-table").click(function(){
							$("#class-form-show").hide();
							$("#right-tables").show();
						});
						showData(dataCache[index]);
					});
					
					$(".table-edit").unbind("click");
					$(".table-edit").click(function(){
						var index = parseInt($(this).attr("dataindex"));
						$("#class-form-show").show();
						$("#right-tables").hide();
						$("#class-form-show").contents().find("input").removeAttr("disabled");
						$("#class-form-show").contents().find("input").css("background-color","#fff");
						$("#class-form-show").contents().find("select").removeAttr("disabled");
						$("#class-form-show").contents().find("select").css("background-color","#fff");
						$("#class-form-show").contents().find("#class-classcode").attr("disabled","disabled");
						$("#class-form-show").contents().find("#class-classcode").css("background-color","#E5E5E5");
						
						$("#class-form-show").contents().find("#class-grade").attr("disabled","disabled");
						$("#class-form-show").contents().find("#class-grade").css("background-color","#E5E5E5");
						$("#class-form-show").contents().find("#class-class_").attr("disabled","disabled");
						$("#class-form-show").contents().find("#class-class_").css("background-color","#E5E5E5");
						$("#class-form-show").contents().find("#class-type").attr("disabled","disabled");
						$("#class-form-show").contents().find("#class-type").css("background-color","#E5E5E5");
						$("#class-form-show").contents().find("#class-teacher-name").attr("disabled","disabled");
						$("#class-form-show").contents().find("#class-teacher-name").css("background-color","#E5E5E5");
						$("#class-form-show").contents().find("#back-to-table").unbind("click");
						$("#class-form-show").contents().find("#back-to-table").click(function(){
							$("#class-form-show").hide();
							$("#right-tables").show();
						});
						showData(dataCache[index]);
						$("#class-form-show").contents().find("#save-btns").unbind("click");
						$("#class-form-show").contents().find("#save-btns").click(function(){
							if(checkClass() && check2("check-fail","class-form-show")){
								updateClass();
							}
						});
						$("#class-form-show").contents().find("#choice-teacher").unbind("click");
						$("#class-form-show").contents().find("#choice-teacher").click(function(){
							teacherChoice(function(){
								$("#teacher-choice-name",parent.parent.document).val("");
								initTeacherTable(1,10000);
								departmentMenu();
								$("#teacher-choice-submit",parent.parent.document).unbind("click");
								$("#teacher-choice-submit",parent.parent.document).click(function(){
									$("#class-form-show").contents().find("#class-teacher-name").val($("#teacher-choice-name",parent.parent.document).val());
									$("#class-form-show").contents().find("#class-teachercode").val($("#teacher-code",parent.parent.document).val());
									$("#tearcher-choice-dialog",parent.parent.document).hide();
									$(".index-dialog-cover",parent.parent.document).hide();
								});
								
							});
						});
						check1("class-grade",isNull,"null","class-form-show");
						check1("class-class_",isNull,"null","class-form-show");
					});
					
					$(".table-modelpower").unbind("click");
					$(".table-modelpower").click(function(){
						var classcode = parseInt($(this).attr("classcode"));
						var classname = $(this).attr("classname");
						$("#class-course-form-show").show();
						$("#right-tables").hide();					
						$("#class-course-form-show").contents().find("#back-to-table").unbind("click");
						$("#class-course-form-show").contents().find("#back-to-table").click(function(){
							$("#class-course-form-show").hide();
							$("#right-tables").show();
						});
						getCourseTable(classcode,classname);
					});
					
					$(".table-delete").unbind("click");
					$(".table-delete").click(function(){
						var classcode = $(this).attr("classcode");
						deleteBox("确定要删除吗？",function(){
							deleteClass(classcode);
						});
					});
				}else{
					content += "<div class=\"table-trs\">" +
									"<div class=\"no-data-show\">没有查到相关结果！</div>" +
								"</div>";
					$("#class-data-show").empty();
					$("#class-data-show").append(content);
				}
			}else{
				content += "<div class=\"table-trs\">" +
								"<div class=\"no-data-show\">没有查到相关结果！</div>" +
							"</div>";
				$("#class-data-show").empty();
				$("#class-data-show").append(content);
			}
		},null,null);	
	}
	function updateClass(){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var grade = $("#class-form-show").contents().find("#class-grade").val();
		var class_ = $("#class-form-show").contents().find("#class-class_").val();
		var data = {
				cmd:"updateClass",
				model:"tclass",
				userKey:userKey,
				data:{
					classcode : $("#class-form-show").contents().find("#class-classcode").val(),
					grade : grade,
					class_ : class_,
					type: $("#class-form-show").contents().find("#class-type option:selected").val(),
					teachercode : $("#class-form-show").contents().find("#class-teachercode").val()
				},
				remark:"",						
		};
		processBegin("正在保存中···");
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				if(res.data.success == "班级存在"){
					processError("修改信息失败，"+grade+"年级"+class_+"班已存在！");
				}else{
					processEnd("修改成功");
				}
			}else{
				processError("修改信息失败");
			}
			closeDialog(function(){
				initClass(currentPage,pageSize);
			});	
		},null,null);
	}
	function deleteClass(classcode){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var data = {
				cmd:"deleteClasses",
				model:"tclass",
				userKey:userKey,
				data:{
					classcode : classcode,
				},
				remark:"",						
		};
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				var content = "";
				for(var key in res.data){
					content += res.data[key]+"</br>";
				}					
				alertBox("nochecked",content,false);
				initClass(currentPage,pageSize);
			}
		},null,null);
	}
	function substr(str,len){
		if(str != undefined){
			if(str.length > len){
				str = str.substring(0,len-2)+"...";
				return str;
			}else{
				return str;
			}
		}else{
			return "";
		}
	}
	function showData(data){
		$("#class-form-show").contents().find("#save-btns-content").show();
		$("#class-form-show").contents().find("#class-classcode").val(data.classcode);
		$("#class-form-show").contents().find("#class-grade").val(data.grade);
		$("#class-form-show").contents().find("#class-class_").val(data.class_);
		$("#class-form-show").contents().find("#class-teacher-name").val(data.teacher.name);
		$("#class-form-show").contents().find("#class-teachercode").val(data.teachercode);
		var len = $("#class-form-show").contents().find("#class-type option");
		for(var i = 0; i < len.length ; i++){
			if(len[i].value == data.type){
				len[i].selected = true;
			}
		}
	}
	
	function getCourseTable(classcode,classname){
		var userKey = $("#user-name",parent.parent.document).attr("key");	
		$("#class-course-form-show").contents().find("#course-table-title").text(classname + "课程表");
		var data = {
				cmd:"getCourseByStudentCode",
				model:"course",
				userKey:userKey,
				data:{
					classcode:classcode
				},
				remark:""
		};
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			var content = "<tr style='font-weight: 700;'>" +
								"<td width='50px'>序号</td>" +
								"<td width='100px'>时间安排</td>" +
								"<td width='100px'>星期一</td>" +
								"<td width='100px'>星期二</td>" +
								"<td width='100px'>星期三</td>" +
								"<td width='100px'>星期四</td>" +
								"<td width='100px'>星期五</td>" +
								"<td width='100px'>星期六</td>" +
								"<td width='100px'>星期日</td>" +
							"</tr>";
			for(var j = 0; j < 8; j++){
				content += "<tr>" + 
						"<td width='50px'>" + (j+1) + "</td>" +
						"<td style='color:#6F6F6F' class='table-course-date' courseno='" + (j+1) + "' width='121px'>" + "" + "</td>" +
						"<td style='color:#6F6F6F' class='table-course-point' coursecode='' subjectcodeno='' courseno='" + (j+1) + "' weekno='1' width='100px'>" + "" + "</td>" +
						"<td style='color:#6F6F6F' class='table-course-point' coursecode='' subjectcodeno='' courseno='" + (j+1) + "' weekno='2' width='100px'>" + "" + "</td>" +
						"<td style='color:#6F6F6F' class='table-course-point' coursecode='' subjectcodeno='' courseno='" + (j+1) + "' weekno='3' width='100px'>" + "" + "</td>" +
						"<td style='color:#6F6F6F' class='table-course-point' coursecode='' subjectcodeno='' courseno='" + (j+1) + "' weekno='4' width='100px'>" + "" + "</td>" +
						"<td style='color:#6F6F6F' class='table-course-point' coursecode='' subjectcodeno='' courseno='" + (j+1) + "' weekno='5' width='100px'>" + "" + "</td>" +
						"<td style='color:#6F6F6F' class='table-course-point' coursecode='' subjectcodeno='' courseno='" + (j+1) + "' weekno='6' width='100px'>" + "" + "</td>" +
						"<td style='color:#6F6F6F' class='table-course-point' coursecode='' subjectcodeno='' courseno='" + (j+1) + "' weekno='7' width='100px'>" + "" + "</td>" +
					"</tr>";
			}
			$("#class-course-form-show").contents().find(".course-table-body").empty();
			$("#class-course-form-show").contents().find(".course-table-body").append(content);
			if(res.result == 1 && res.data != undefined && res.data != ""){
				var data = res.data;
				var list = data.course;
				for(var i = 0; i < list.length; i++){
					for(var key in list[i]){
						if(list[i][key] == null || list[i][key] == "null"){
							list[i][key] = "";
						}
					}
					var courseIndex = parseInt(list[i]["courseno"]*9) + parseInt(list[i]["weekno"]) + 1;
					var timeIndex = parseInt(list[i]["courseno"]*9) + 1;
					var subjectcodeno = list[i]["subjectcodeno"];
					var coursecode = list[i]["coursecode"];
					$("#class-course-form-show").contents().find(".course-table-body td")[courseIndex].innerHTML = list[i]["subjectcode"];
					$("#class-course-form-show").contents().find(".course-table-body td")[courseIndex].setAttribute("subjectcodeno",subjectcodeno);
					$("#class-course-form-show").contents().find(".course-table-body td")[courseIndex].setAttribute("coursecode",coursecode);
					$("#class-course-form-show").contents().find(".course-table-body td")[timeIndex].innerHTML = list[i]["coursetime"];
				}
			}
			
		},null,null);
	}	
	
	function checkClass(){
		if($("#class-form-show").contents().find("#class-grade").val() == ""){
			$("#class-form-show").contents().find("#class-grade").focus();
			$("#class-form-show").contents().find("#class-grade-ex").html("不能为空");
			return false; 
		}else if($("#class-form-show").contents().find("#class-class_").val() == ""){
			$("#class-form-show").contents().find("#class-class_").focus();
			$("#class-form-show").contents().find("#class-class_-ex").html("不能为空");
			return false; 
		}else{
			return true;
		}
	}
	function pagebtns(){
		$("#class-search-btn").click(function(){
			now = 0;
			initClass(1,pageSize);
		});		
	}
	function initSmPages(){
		var rcH = $(".body-right",parent.document).height() - $(".right-title").height()-3;
		$(".right-content").height(rcH);
		
		var cdsH = $(".right-content").height() - $(".content-title").height() - 40;
		$(".content-data-show").height(cdsH);
	}
})();